<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据号" prop="serialNo">
        <el-input
          v-model="queryParams.serialNo"
          placeholder="请输入单据号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择业务类型" clearable filterable size="small">
          <el-option
            v-for="dict in bizTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable filterable size="small">
          <el-option v-for="item in supplierNameList" :label="item.name" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable filterable size="small">
          <el-option
            v-for="dict in contractTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="applyUser">
        <el-select v-model="queryParams.applyUser" placeholder="请选择申请人" clearable filterable size="small">
          <el-option
            v-for="dict in applyUserOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请日期">
        <el-date-picker
          v-model="daterangeApplyTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="申请状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="请选择申请状态" clearable filterable size="small">
          <el-option
            v-for="dict in applyStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUser">
        <el-select v-model="queryParams.auditUser" placeholder="请选择审核人" clearable filterable size="small">
          <el-option
            v-for="dict in auditUserOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核日期">
        <el-date-picker
          v-model="daterangeAuditTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable filterable size="small">
          <el-option
            v-for="dict in auditStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库状态" prop="billStatus">
        <el-select v-model="queryParams.billStatus" placeholder="请选择入库状态" clearable filterable size="small">
          <el-option
            v-for="dict in billStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wmsApply:inWarahouseApply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wmsApply:inWarahouseApply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wmsApply:inWarahouseApply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wmsApply:inWarahouseApply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inWarahouseApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="申请单号" align="center" prop="serialNo" />

      <el-table-column label="关联单据号" align="center" prop="serialNo" />
      
      <el-table-column label="业务类型" align="center" prop="bizType" :formatter="bizTypeFormat" />

      <el-table-column label="供应商" align="center" prop="name" :formatter="name" />
      <el-table-column label="合同号" align="center" prop="contractNo" :formatter="contractNo" />
      <el-table-column label="合同类型" align="center" prop="contractType" :formatter="contractTypeFormat" />
      <el-table-column label="单据状态" align="center" prop="billStatus" :formatter="billStatusFormat" />
      <el-table-column label="申请人" align="center" prop="applyUser" :formatter="applyUserFormat" />
      <el-table-column label="申请日期" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus" :formatter="applyStatusFormat" />
      <el-table-column label="审核人" align="center" prop="auditUser" :formatter="auditUserFormat" />
      <el-table-column label="审核日期" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="auditStatusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.applyStatus == 1 || (scope.row.applyStatus == 2 && scope.row.billStatus) || scope.row.applyStatus == 0"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wmsApply:inWarahouseApply:edit']"
          >修改</el-button>
          <el-button type="text" v-if="scope.row.applyStatus == 2 && scope.row.billStatus == 0" @click="handlebillStatus(scope.row)" icon="el-icon-s-promotion">
            入库
          </el-button>
          <el-button
            size="mini"
            v-if="scope.row.applyStatus == 0"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wmsApply:inWarahouseApply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" :inline="true">
        <el-form-item label="单据号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入单据号" disabled/>
        </el-form-item>
        <el-form-item label="关联单据号" prop="bizBillId">
          <el-input v-model="form.bizBillId" placeholder="请输入关联单据号" disabled>
            <el-button slot="append"  @click="getFinishList">
              选择
            </el-button>
          </el-input>

        </el-form-item>
        <el-form-item label="业务类型" prop="bizType">
          <el-select v-model="form.bizType" placeholder="请选择业务类型" clearable filterable>
            <el-option
              v-for="dict in bizTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择供应商" clearable filterable disabled>
            <el-option v-for="item in supplierNameList" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同号" prop="contractId">
          <el-input v-model="form.contractNo" placeholder="请输入合同号" disabled/>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型" clearable filterable disabled>
            <el-option
              v-for="dict in contractTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请类型" prop="applyType">
          <el-select v-model="form.applyType" placeholder="请选择申请类型" clearable filterable disabled>
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请人" prop="applyUser">
          <el-select v-model="form.applyUser" placeholder="请选择申请人" clearable filterable disabled>
            <el-option
              v-for="dict in applyUserOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期" prop="applyTime">
          <el-date-picker clearable size="small"
          disabled
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请状态" prop="applyStatus">
          <el-select v-model="form.applyStatus" placeholder="请选择申请状态" clearable filterable disabled>
            <el-option
              v-for="dict in applyStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人" prop="auditUser">
          <el-select v-model="form.auditUser" placeholder="请选择审核人" clearable filterable disabled>
            <el-option
              v-for="dict in auditUserOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核日期" prop="auditTime">
          <el-date-picker clearable size="small"
          disabled
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态" clearable filterable disabled>
            <el-option
              v-for="dict in auditStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditComment">
          <el-input v-model="form.auditComment" placeholder="请输入审核意见" disabled/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" disabled/>
        </el-form-item>
        <el-form-item label="入库状态" prop="billStatus">
          <el-select v-model="form.billStatus" placeholder="请选择入库状态" clearable filterable disabled>
            <el-option
              v-for="dict in billStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">入库申请明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWmsInWarehouseApplyDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWmsInWarehouseApplyDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wmsInWarehouseApplyDetailList" :row-class-name="rowWmsInWarehouseApplyDetailIndex" @selection-change="handleWmsInWarehouseApplyDetailSelectionChange" ref="wmsInWarehouseApplyDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料档案编号" prop="materialId" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.materialId'" :rules="rules.materialId">
              <el-input v-model="scope.row.materialId" placeholder="请输入物料档案编号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="仓库编号" prop="warehouseId" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.warehouseId'" :rules="rules.warehouseId">
              <el-input v-model="scope.row.warehouseId" placeholder="请输入仓库编号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="库位编号" prop="whLocationId" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.whLocationId'" :rules="rules.whLocationId">
              <el-input v-model="scope.row.whLocationId" placeholder="请输入库位编号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="库区编号" prop="whRegionId" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.whRegionId'" :rules="rules.whRegionId">
              <el-input v-model="scope.row.whRegionId" placeholder="请输入库区编号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="到货数量" prop="totalQuantity" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.totalQuantity'" :rules="rules.totalQuantity">
              <el-input v-model="scope.row.totalQuantity" placeholder="请输入到货数量" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="入库数量" prop="inQuantity" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.inQuantity'" :rules="rules.inQuantity">
              <el-input v-model="scope.row.inQuantity" placeholder="请输入入库数量" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="序列号" prop="serialNo" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.serialNo'" :rules="rules.serialNo">
              <el-input v-model="scope.row.serialNo" placeholder="请输入序列号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="生产批号" prop="batchNo" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.batchNo'" :rules="rules.batchNo">
              <el-input v-model="scope.row.batchNo" placeholder="请输入生产批号" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" prop="productDate" width="240">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.productDate'" :rules="rules.productDate">
              <el-date-picker clearable v-model="scope.row.productDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择生产日期" />
             </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="生产单位" prop="manufacturer" width="150">
            <template slot-scope="scope">
             <el-form-item :prop="'wmsInWarehouseApplyDetailList.'+scope.$index+'.manufacturer'" :rules="rules.manufacturer">
              <el-input v-model="scope.row.manufacturer" placeholder="请输入生产单位" />
             </el-form-item>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择生产完工单" :visible.sync="openFinish">
        <el-form :model="queryParamsFinish" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="生产计划编号" prop="planId">
            <el-input
              v-model="queryParams.planSerialNo"
              placeholder="请输入生产计划编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="生产作业" prop="jobId">
            <el-input
              v-model="queryParams.jobName"
              placeholder="请输入生产作业编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产品" prop="materialId">
            <el-input
              v-model="queryParams.infoName"
              placeholder="请输入产品id"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="完工日期" prop="finishDate">
            <el-date-picker clearable size="small"
              v-model="queryParams.finishDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择完工日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="生产批号" prop="batchNo">
            <el-input
              v-model="queryParams.batchNo"
              placeholder="请输入生产批号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable filterable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['mesProduct:productFinish:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['mesProduct:productFinish:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['mesProduct:productFinish:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['mesProduct:productFinish:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="finishList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="编号"/>
          <el-table-column label="单号" align="center" prop="serialNo" >
          </el-table-column>
          <el-table-column label="生产计划" align="center" prop="planSerialNo" />
          <el-table-column label="生产作业" align="center" prop="jobName" />
          <!-- <el-table-column label="计划排产编号" align="center" prop="scheduleId" /> -->
          <el-table-column label="产品" align="center" prop="infoName" />
          <el-table-column label="型号" align="center" prop="model" />
          <el-table-column label="规格" align="center" prop="specification" />
          <el-table-column label="单位" align="center" prop="unit" />
          <el-table-column label="完工日期" align="center" prop="finishDate" width="180">
            <template slot-scope="scope">

              <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="生产数量" align="center" prop="finishQuantity" />
          <el-table-column label="生产批号" align="center" prop="batchNo" />
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handlFinishSelect(scope.row)"
                v-hasPermi="['mesProduct:productFinish:edit']"
              >选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

    </el-dialog>

  </div>
</template>

<script>
import { getSupplierNameAPI,listInWarahouseApply, getInWarahouseApply, delInWarahouseApply, addInWarahouseApply, updateInWarahouseApply, exportInWarahouseApply,applyBillStatusAPI } from "@/api/wmsApply/inWarahouseApply";
import { listProductFinish, getProductFinish, delProductFinish, addProductFinish, updateProductFinish, exportProductFinish } from "@/api/mesProduct/productFinish";



export default {
  name: "InWarahouseApply",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsInWarehouseApplyDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库申请表格数据
      inWarahouseApplyList: [],
      // 入库申请明细表格数据
      wmsInWarehouseApplyDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 业务类型字典
      bizTypeOptions: [],
      // 合同类型字典
      contractTypeOptions: [],
      // 申请人字典
      applyUserOptions: [],
      // 申请日期时间范围
      daterangeApplyTime: [],
      // 申请状态字典
      applyStatusOptions: [],
      // 审核人字典
      auditUserOptions: [],
      // 审核日期时间范围
      daterangeAuditTime: [],
      // 审核状态字典
      auditStatusOptions: [],
      // 入库状态字典
      billStatusOptions: [],

      finishList:[],
      //选择生产完工单
      openFinish:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNo: null,
        bizType: null,
        supplierId: null,
        contractType: null,
        applyUser: null,
        applyTime: null,
        applyStatus: null,
        auditUser: null,
        auditTime: null,
        auditStatus: null,
        billStatus: null
      },

      //请求生产完工单方法
      queryParamsFinish: {
        pageNum: 1,
        pageSize: 10,
        planId: null,
        jobId: null,
        materialId: null,
        finishDate: null,
        batchNo: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      supplierNameList:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("event_type").then(response => {
      this.bizTypeOptions = response.data;
    });
    this.getDicts("contract_type").then(response => {
      this.contractTypeOptions = response.data;
    });
    this.getDicts("mes_auditor").then(response => {
      this.applyUserOptions = response.data;
    });
    this.getDicts("apply_status").then(response => {
      this.applyStatusOptions = response.data;
    });
    this.getDicts("mes_auditor").then(response => {
      this.auditUserOptions = response.data;
    });
    this.getDicts("audit_status").then(response => {
      this.auditStatusOptions = response.data;
    });
    this.getDicts("in_warehouse_status").then(response => {
      this.billStatusOptions = response.data;
    });
    this.getSupplier();
  },
  watch: {
    'wmsInWarehouseApplyDetailList': {
      handler(newVal, oldVal) {
        this.form.wmsInWarehouseApplyDetailList = newVal;
      },
      immediate: true,
      deep: true,
    }
  },
  methods: {

    handlFinishSelect(row){
      console.log(row);
      this.openFinish = false
    },
    async getFinishList(){
      const res = await listProductFinish(this.queryParamsFinish);
      this.finishList = res.rows;
      this.openFinish = true;
    },

    async getSupplier(){
      const res = await getSupplierNameAPI();
      this.supplierNameList = res.data;
    },

    /** 查询入库申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeApplyTime && '' != this.daterangeApplyTime) {
        this.queryParams.params["beginApplyTime"] = this.daterangeApplyTime[0];
        this.queryParams.params["endApplyTime"] = this.daterangeApplyTime[1];
      }
      if (null != this.daterangeAuditTime && '' != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] = this.daterangeAuditTime[0];
        this.queryParams.params["endAuditTime"] = this.daterangeAuditTime[1];
      }
      listInWarahouseApply(this.queryParams).then(response => {
        this.inWarahouseApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 业务类型字典翻译
    bizTypeFormat(row, column) {
      return this.selectDictLabel(this.bizTypeOptions, row.bizType);
    },
    // 合同类型字典翻译
    contractTypeFormat(row, column) {
      return this.selectDictLabel(this.contractTypeOptions, row.contractType);
    },
    // 申请人字典翻译
    applyUserFormat(row, column) {
      return this.selectDictLabel(this.applyUserOptions, row.applyUser);
    },
    // 申请状态字典翻译
    applyStatusFormat(row, column) {
      return this.selectDictLabel(this.applyStatusOptions, row.applyStatus);
    },
    // 审核人字典翻译
    auditUserFormat(row, column) {
      return this.selectDictLabel(this.auditUserOptions, row.auditUser);
    },
    // 审核状态字典翻译
    auditStatusFormat(row, column) {
      return this.selectDictLabel(this.auditStatusOptions, row.auditStatus);
    },
    // 入库状态字典翻译
    billStatusFormat(row, column) {
      return this.selectDictLabel(this.billStatusOptions, row.billStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        bizBillId: null,
        bizType: null,
        supplierId: null,
        contractId: null,
        contractType: null,
        applyType: null,
        applyUser: null,
        applyTime: null,
        applyStatus: null,
        auditUser: null,
        auditTime: null,
        auditStatus: null,
        auditComment: null,
        remark: null,
        billStatus: null,
        wmsInWarehouseApplyDetailList: [],
      };
      this.wmsInWarehouseApplyDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeApplyTime = [];
      this.daterangeAuditTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加入库申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInWarahouseApply(id).then(response => {
        this.form = response.data;
        this.wmsInWarehouseApplyDetailList = response.data.wmsInWarehouseApplyDetailList;
        this.open = true;
        this.title = "修改入库申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          
          if (this.form.id != null) {
            updateInWarahouseApply(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInWarahouseApply(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除入库申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInWarahouseApply(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },

    handlebillStatus(row) {
      const ids = row.id;
      this.$confirm('是否确认入库编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return applyBillStatusAPI(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("入库成功");
        })
    },
	/** 入库申请明细序号 */
    rowWmsInWarehouseApplyDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 入库申请明细添加按钮操作 */
    handleAddWmsInWarehouseApplyDetail() {
      let obj = {};
      obj.materialId = "";
      obj.warehouseId = "";
      obj.whLocationId = "";
      obj.whRegionId = "";
      obj.totalQuantity = "";
      obj.inQuantity = "";
      obj.serialNo = "";
      obj.batchNo = "";
      obj.productDate = "";
      obj.manufacturer = "";
      this.wmsInWarehouseApplyDetailList.push(obj);
    },
    /** 入库申请明细删除按钮操作 */
    handleDeleteWmsInWarehouseApplyDetail() {
      if (this.checkedWmsInWarehouseApplyDetail.length == 0) {
        this.$alert("请先选择要删除的入库申请明细数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.wmsInWarehouseApplyDetailList.splice(this.checkedWmsInWarehouseApplyDetail[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleWmsInWarehouseApplyDetailSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.wmsInWarehouseApplyDetail.clearSelection();
        this.$refs.wmsInWarehouseApplyDetail.toggleRowSelection(selection.pop());
      } else {
        this.checkedWmsInWarehouseApplyDetail = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有入库申请数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInWarahouseApply(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>